import 'package:barcode_scan2/model/scan_options.dart';
import 'package:barcode_scan2/model/scan_result.dart';
import 'package:barcode_scan2/platform_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

// 扫一扫
class ToolsScan {
  // 扫码
  static Future<void> scan() async {
    // 现在时间
    final currentTime = DateTime.now().hour;
    // 控制灯光
    bool light = currentTime < 07 || currentTime > 20;
    // ScanOptions设置闪光灯和前后摄像头
    var options = ScanOptions(autoEnableFlash: light, strings: {
      'cancel': '关闭',
      'flash_on': '打开灯光',
      'flash_off': '关闭灯光',
    });
    // 返回扫描的参数
    ScanResult scanResult = await BarcodeScanner.scan(options: options);
    // 返回参数
    String result = scanResult.rawContent;
    if (result.isEmpty) {
      Get.back();
    } else {
      // 执行扫码
      doScan(result);
    }
  }

  static doScan(String result) {
    Get.to(_WidgetScan(result));
  }
}

// 扫码页面
class _WidgetScan extends StatelessWidget {
  final String result;
  const _WidgetScan(this.result);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        title: const Text('扫一扫'),
      ),
      body: Center(
        child: InkWell(
          onLongPress: () {
            Clipboard.setData(ClipboardData(text: result));
            EasyLoading.showToast('文本已复制');
          },
          child: Text(result),
        ),
      ),
    );
  }
}
